package Twitter.RequestSender;

import java.util.Iterator;
import java.util.Set;

import twitter.Server.ServerType;
import twitter.TwitterLibrary.SocketConfig;
import twitter.TwitterLibrary.Utility;

public class ServerRegistrationSender implements Runnable{
	private Utility utility;
	private RequestSender sender;
	//send all the server information on the primary server to other servers every 5 seconds
	public ServerRegistrationSender(Utility utility)
	{
		this.utility=utility;
		sender=new RequestSender(utility);
	}
	@Override
	public void run() {
		while(true)
		{
			
		try {
			
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// TODO Auto-generated method stub
		String notice="";

		if(SocketConfig.serverType==ServerType.PrimaryServer)
		{
			notice=utility.constructNotice(true);
		}
		else
		{
			notice=utility.constructNotice(false);
		}
		String serverRegistration=notice+"\r\n registration \r\n";
		System.out.println("||||||||||||||||registrate server "+serverRegistration);
		sender.sendRequestToEveryServer(SocketConfig.primaryIp, serverRegistration);
		}
		
	}

}
